package itsm.isperp.module.controller.sparepart;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseProcessController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.project.ProjProject;
import itsm.isperp.module.entity.sparepart.SptSparepartBack;
import itsm.isperp.module.entity.sparepart.SptSparepartIn;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.project.ProjProjectMemberService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.sparepart.SptSparepartBackService;
import itsm.isperp.module.service.sparepart.SptSparepartChangeDetailService;
import itsm.isperp.workflow.service.BpmProcessRunService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 备件归还管理控制器
 * 
 * @author lizx
 * @date 2013-10-6
 */
@Controller
@RequestMapping("sparepart/sparepartBack")
public class SptSparepartBackController extends
		BaseProcessController<SptSparepartBack> {

	@Autowired
	protected SptSparepartBackService sptSparepartBackService;

	@Autowired
	ProjProjectMemberService projProjectMemberService;

	@Autowired
	protected ProjProjectService projProjectService;

	@Override
	public SptSparepartBackService getService() {
		return sptSparepartBackService;
	}

	@Override
	public String getPageTitle() {
		return "备件归还";
	}

	@Override
	public String getProceessKey() {
		return "sparepartBackProcess";
	}

	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();

		SptSparepartBack model = null;

		AppUserService appUserService = ContextHolder
				.getSpringBean("appUserService");

		IUser user = ContextHolder.getLoginUser();
		if (StringUtils.isEmpty(id)) {
			model = new SptSparepartBack();
			model.setId(HibernateUUIDGenerator.generate());
			model.setCreator(user.getUsername());

			model.setBackDate(new Date());

			model.setBackManId(user.getUsername());
			model.setBackManName(user.getFullname());

			mv.addObject("creatorName", user.getFullname());

		} else {
			model = this.getService().findOne(id);

			mv.addObject("creatorName",
					appUserService.findByUsername(model.getCreator())
							.getFullname());
		}

		mv.addObject("model", model);
		String projectId=req.getParameter("projectId");
		if(StringUtils.isNotEmpty(projectId))
		{
			List<ProjProject> list=new ArrayList<ProjProject>();
			list.add(projProjectService.findOne(projectId));
			mv.addObject("projects",list
					);
		}else
		{
			mv.addObject("projects",
					projProjectService.findAllPublished("id", "name"));
		}
		String fromId=req.getParameter("fromId");
		String fromProcessKey=req.getParameter("fromProcessKey");
		if(StringUtils.isNotEmpty(fromId))
		{
			mv.addObject("fromId", fromId);
			mv.addObject("fromProcessKey",fromProcessKey);
		}

		return mv;

	}

	@RequestMapping("delete")
	@ResponseBody
	@Override
	public ResultMessage delete(String id, HttpServletRequest req) {

		BpmProcessRunService bpmProcessRunService = ContextHolder
				.getSpringBean("bpmProcessRunService");
		String process_Id = (String) this.getService()
				.findOneToMap(id, "process_Id").get("process_Id");

		if (StringUtils.isNotEmpty(process_Id))
			bpmProcessRunService.deleteProcessInstance(process_Id, "删除",
					ContextHolder.getLoginUsername());

		SptSparepartChangeDetailService sptSparepartChangeDetailService = ContextHolder
				.getSpringBean("sptSparepartChangeDetailService");

		sptSparepartChangeDetailService.deleteByRelationId(id);

		this.getService().delete(id);

		return ResultMessage.newInstance(true);
	}

	@Override
	@RequestMapping(value = { "manage", "" })
	public ModelAndView manage(HttpServletRequest req) {
		ModelAndView mv = super.manage(req);

		// 项目
		DataRequest dataProject = new JqGridRequest();
		dataProject.isPageable(false);
		dataProject.setSelectColumns("id", "name");
		mv.addObject("projects", projProjectService.findAll(dataProject)
				.getContent());

		return mv;
	}

	@RequestMapping("backSptlist")
	@ResponseBody
	public PageResponse<Map<String, Object>> outSptlist(String sparepartId,
			JqGridRequest request, HttpServletRequest req) {
		return sptSparepartBackService.findAllSptDetailByRequest(sparepartId,
				request);
	}
	
	@RequestMapping("backStoreEdit")
	public ModelAndView backStoreEdit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();
		SptSparepartBack model = this.getService().findOne(id);
		mv.addObject("model", model);
		String projectId=model.getProjectId();
		if(StringUtils.isNotEmpty(projectId))
		{
			List<ProjProject> list=new ArrayList<ProjProject>();
			list.add(projProjectService.findOne(projectId));
			mv.addObject("projects",list
					);
		}else
		{
			mv.addObject("projects",
					projProjectService.findAllPublished("id", "name"));
		}
		return mv;

	}
}
